package com.have.blog.xo.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.commons.feign.PictureFeignClient;
import com.have.blog.model.api.CommonPage;
import com.have.blog.model.mapper.TResourceSortMapper;
import com.have.blog.model.mapper.TStudyVideoMapper;
import com.have.blog.model.models.TResourceSort;
import com.have.blog.model.models.TStudyVideo;
import com.have.blog.model.models.TStudyVideoExample;
import com.have.blog.utils.MyStringUtils;
import com.have.blog.utils.ResultUtils;
import com.have.blog.xo.constant.MessageConstant;
import com.have.blog.xo.constant.SysConstant;
import com.have.blog.xo.service.admin.StudyVideoService;
import com.have.blog.xo.uitls.WebUtil;
import com.have.blog.xo.vo.StudyVideoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
@SuppressWarnings("all")
public class StudyVideoServiceImpl implements StudyVideoService {
    @Autowired
    private TStudyVideoMapper videoMapper;
    @Autowired
    private WebUtil webUtil;
    @Autowired
    private PictureFeignClient pictureFeignClient;
    @Autowired
    private TResourceSortMapper resourceSortMapper;

    @Override
    public String getPageList(StudyVideoVo studyVideoVO) {
        PageHelper.startPage(studyVideoVO.getCurrentPage(), studyVideoVO.getPageSize());
        TStudyVideoExample example = new TStudyVideoExample();
        TStudyVideoExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(studyVideoVO.getKeyword()) && !StringUtils.isEmpty(studyVideoVO.getKeyword().trim())) {
            criteria.andNameLike("%" + studyVideoVO.getKeyword() + "%");
        }
        criteria.andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        example.setOrderByClause("create_time desc");
        List<TStudyVideo> list = videoMapper.selectByExample(example);

        StringBuilder sb = new StringBuilder();
        list.forEach(item -> {
            if (!StringUtils.isEmpty(item.getFileUid())) {
                sb.append(item.getFileUid() + SysConstant.FILE_SEGMENTATION);
            }
        });
        String pictureResult = null;
        Map<String, String> pictureMap = new HashMap<>();
        if (!StringUtils.isEmpty(sb.toString())) {
            pictureResult = this.pictureFeignClient.getPicture(sb.toString(), SysConstant.FILE_SEGMENTATION);
        }
        if (pictureResult == null) {
            for (TStudyVideo item : list) {
                //获取分类资源
                if (!StringUtils.isEmpty(item.getFileUid())) {
                    List<String> pictureUidsTemp = MyStringUtils.changeStringToStringList(item.getFileUid(), SysConstant.FILE_SEGMENTATION);
                    List<String> pictureListTemp = new ArrayList<>();
                    pictureUidsTemp.forEach(picture -> {
                        pictureListTemp.add(pictureMap.get(picture));
                    });
                    item.setPhotoList(pictureListTemp);
                }
            }
        } else {
            List<Map<String, Object>> picList = webUtil.getPictureMap(pictureResult);
            picList.forEach(item -> {
                pictureMap.put(item.get(SysConstant.UID).toString(), item.get(SysConstant.URL).toString());
            });

            for (TStudyVideo item : list) {
                //获取分类资源
                if (!StringUtils.isEmpty(item.getFileUid())) {
                    List<String> pictureUidsTemp = MyStringUtils.changeStringToStringList(item.getFileUid(), SysConstant.FILE_SEGMENTATION);
                    List<String> pictureListTemp = new ArrayList<>();
                    pictureUidsTemp.forEach(picture -> {
                        pictureListTemp.add(pictureMap.get(picture));
                    });
                    item.setPhotoList(pictureListTemp);
                }

                if (!StringUtils.isEmpty(item.getResourceSortUid())) {
                    TResourceSort resourceSort = resourceSortMapper.selectByPrimaryKey(item.getResourceSortUid());
                    item.setResourceSort(resourceSort);
                }
            }
        }
        return ResultUtils.successWithData(CommonPage.restPage(list));
    }

    @Override
    public String addStudyVideo(StudyVideoVo studyVideoVO) {
        TStudyVideo studyVideo = new TStudyVideo();
        studyVideo.setUid(MyStringUtils.getUUID());
        studyVideo.setName(studyVideoVO.getName());
        studyVideo.setSummary(studyVideoVO.getSummary());
        studyVideo.setContent(studyVideoVO.getContent());
        studyVideo.setFileUid(studyVideoVO.getFileUid());
        studyVideo.setBaiduPath(studyVideoVO.getBaiduPath());
        studyVideo.setResourceSortUid(studyVideoVO.getResourceSortUid());
        studyVideo.setClickCount("0");
        studyVideo.setUpdateTime(new Date());
        studyVideo.setCreateTime(new Date());
        int result = videoMapper.insert(studyVideo);
        if (result > 0) {
            return ResultUtils.successWithMessage(MessageConstant.INSERT_SUCCESS);
        }
        return ResultUtils.successWithMessage(MessageConstant.INSERT_FAIL);

    }

    @Override
    public String editStudyVideo(StudyVideoVo studyVideoVO) {
        TStudyVideo studyVideo = videoMapper.selectByPrimaryKey(studyVideoVO.getUid());
        studyVideo.setName(studyVideoVO.getName());
        studyVideo.setSummary(studyVideoVO.getSummary());
        studyVideo.setContent(studyVideoVO.getContent());
        studyVideo.setFileUid(studyVideoVO.getFileUid());
        studyVideo.setBaiduPath(studyVideoVO.getBaiduPath());
        studyVideo.setResourceSortUid(studyVideoVO.getResourceSortUid());
        studyVideo.setUpdateTime(new Date());
        videoMapper.updateByPrimaryKeySelective(studyVideo);
        return ResultUtils.successWithMessage(MessageConstant.UPDATE_SUCCESS);
    }

    @Override
    public String deleteBatchStudyVideo(List<StudyVideoVo> studyVideoVOList) {
        if (CollectionUtils.isEmpty(studyVideoVOList)) {
            return ResultUtils.errorWithMessage(MessageConstant.PARAM_INCORRECT);
        }
        List<String> uids = new ArrayList<>();
        studyVideoVOList.forEach(item -> {
            uids.add(item.getUid());
        });

        TStudyVideoExample example = new TStudyVideoExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andUidIn(uids);
        TStudyVideo template = new TStudyVideo();
        template.setStatus(BlogStatus.DISABLE.getStatus());
        template.setUpdateTime(new Date());
        int result = videoMapper.updateByExampleSelective(template, example);

        if (result > 0) {
            return ResultUtils.successWithMessage(MessageConstant.DELETE_SUCCESS);
        } else {
            return ResultUtils.errorWithMessage(MessageConstant.DELETE_FAIL);
        }
    }
}
